﻿@{
    ViewBag.Title = "云商城";
    Layout = "~/Views/Shared/_MallLayout.cshtml";
    string img_url = ViewBag.img_url;
    List<token_shop_subject_dto> topicList = ViewBag.topic;//专题
    List<ad_dto> topBanner = ViewBag.banner;//头部广告
    List<ad_dto> middleBanner = ViewBag.middleBanner;//中部广告
    List<ad_dto> lowerBanner = ViewBag.lowerBanner;//中下部广告
    List<activity_dto> timeBuyActivityList = ViewBag.timeBuyActivityList;//首页限时购活动
    red_user_dto redcord = ViewBag.redcord as red_user_dto;
    merchant_dto merchant = ViewBag.merchant as merchant_dto;
    string link = "/Topic/TopicProduct?subject_id=";
    string fromPage = string.Empty;
}
<link href="~/Content/css/tokenmall/css/reg.css" rel="stylesheet" />
<style type="text/css">
    .mescroll {
        position: fixed;
        top: 0;
        bottom: 0;
        height: auto;
    }
</style>
<body class="">
    <div class="mescroll iphonex" id="mescroll">
        <div class="page-home bg-white">
            <!-- 头部、搜索、幻灯片 -->
            <div class="head">
                <div class="search flex" style="line-height:1.38rem;">
                    <span class="ewm fn-right" onclick="richScan()">
                        <img src="~/Content/css/tokenmall/img/icon/ewm.png" alt="">
                    </span>
                    <div class="mod-search flex-1">
                        <div class="searchBox bg-white radius flex">
                            <div class="searchBtn flex-box">
                                <i class="icon-sc"></i>
                            </div>
                            <form action="javascript:void(0);" style="display:inline-block;width:100%;">
                                <span class="searchInput fn28 flex-1 col-gray search_btn">请输入需要搜索的商品</span>
                            </form>
                        </div>
                    </div>
                    <span class="contact fn-right">
                        <a href="javascript:void(0);" class="contactUs">
                            <img src="~/Content/css/tokenmall/img/icon/contact.png" />
                        </a>
                    </span>
                </div>
                <div class="swiper-container mod-swiper">
                    <div class="swiper-wrapper">
                        @if (topBanner != null && topBanner.Count() > 0) {
                            foreach (var banner in topBanner) {
                                <div class="swiper-slide">
                                    <a href="@banner.link_url">
                                        <img src="@(img_url + banner.img_url)" />
                                    </a>
                                </div>
                            }

                        } else {
                            <div class="swiper-slide">
                                <a href="">
                                    <img src="~/Content/css/tokenmall/img/ad1.jpg" />
                                </a>
                            </div>
                        }
                    </div>
                    <div class="swiper-pagination fn26"></div>
                </div>
            </div>

            <div class="block">
                <!-- 专题 -->
                <div class="nav mt30">
                    @if (topicList != null && topicList.Count > 0) {
                        foreach (var list in topicList) {
                            if (list.type == ETokenShopSubjectType.Fixed) {
                                <a href="javascript:void(0);">
                                    <span>
                                        <img src="@(img_url + list.img_url)" />
                                    </span>
                                    <p>@list.title</p>
                                </a>
                            } else {
                                if (list.topic_link == null || list.topic_link == "") {
                                    <a href="@(link + list.id + "&topicTitle=" + list.title)">
                                        <span>
                                            <img src="@(img_url + list.img_url)" />
                                        </span>
                                        <p>@list.title</p>
                                    </a>
                                } else {
                                    <a href="@(list.topic_link)">
                                        <span>
                                            <img src="@(img_url + list.img_url)" />
                                        </span>
                                        <p>@list.title</p>
                                    </a>
                                }
                            }
                        }
                    }
                </div>

                <!-- 广告图 -->
                @if (middleBanner != null && middleBanner.Count() > 0) {
                    var banner = middleBanner.FirstOrDefault();
                    <div class="mb30">
                        <a href="@banner.link_url">
                            <img src="@(img_url + banner.img_url)" />
                        </a>
                    </div>
                }
                @if (lowerBanner != null && lowerBanner.Count() > 0) {
                    ad_dto banner1 = lowerBanner[0];
                    ad_dto banner2 = null;
                    ad_dto banner3 = null;
                    ad_dto banner4 = null;
                    if (lowerBanner.Count() > 1) {
                        banner2 = lowerBanner[1];
                    }
                    if (lowerBanner.Count() > 2) {
                        banner3 = lowerBanner[2];
                    }
                    if (lowerBanner.Count() > 3) {
                        banner4 = lowerBanner[3];
                    }
                    <div class="ad1 flex mb15">
                        <div class="flex-1">
                            <a href="@banner1.link_url">
                                <img src="@(img_url + banner1.img_url)" />
                            </a>
                        </div>
                        @if (banner2 != null) {
                            <div class="flex-1">
                                <div class="mb15">
                                    <a href="@banner2.link_url">
                                        <img src="@(img_url + banner2.img_url)" />
                                    </a>
                                </div>
                                @if (banner3 != null) {
                                    <div>
                                        <a href="@banner3.link_url">
                                            <img src="@(img_url + banner3.img_url)" />
                                        </a>
                                    </div>
                                }
                            </div>
                        }
                    </div>
                    if (banner4 != null) {
                        <div class="ad1 mb15">
                            <a href="@banner4.link_url">
                                <img src="@(img_url + banner4.img_url)" />
                            </a>
                        </div>
                    }
                }

            </div>
            <!-- 活动 -->
            @if (timeBuyActivityList != null && timeBuyActivityList.Count() > 0) {
                <div id="activity_list_div" class="activity">
                    <div class="innerBox j-activity">
                        <div class="swiper-wrapper">
                            @foreach (var item in timeBuyActivityList) {
                                if (item.begin_time > DateTime.Now && item.end_time > DateTime.Now) {
                                    item.status = EActivityStatus.NotStarted;
                                } else if (item.begin_time <= DateTime.Now && item.end_time >= DateTime.Now) {
                                    item.status = EActivityStatus.Underway;
                                } else {
                                    item.status = EActivityStatus.Finished;
                                }

                                <div class="li fn24 swiper-slide j-cur @(item.status == EActivityStatus.Underway ? "bg1 underway_activity_item" : "bg2") activity_item_div"
                                     data-id="@item.id" data-endtime="@(item.end_time.ToString("yyyy/MM/dd HH:mm:ss"))" data-status="@((int)item.status)">
                                    <div class="fn-center">
                                        @if (item.status == EActivityStatus.Finished) {
                                            <div class="top mt20">
                                                <span class="btn fn-bold fn-center">@(item.begin_time.ToString("MM/dd HH:mm"))</span>
                                            </div>
                                            <div class="text">
                                                <span class="">已结束</span>
                                            </div>
                                        } else if (item.status == EActivityStatus.Underway) {
                                            <div class="top mt20">
                                                <span class="btn fn-bold fn-center">@(item.begin_time.ToString("MM/dd HH:mm"))</span>
                                            </div>
                                            <div class="text">
                                                <span class="">进行中</span>
                                            </div>
                                        } else if (item.status == EActivityStatus.NotStarted) {
                                            <div class="top mt20">
                                                <span class="btn fn-bold fn-center">@(item.begin_time.ToString("MM/dd HH:mm"))</span>
                                            </div>
                                            <div class="text">
                                                <span class="">即将开始</span>
                                            </div>
                                        }
                                    </div>
                                    <i class="jiao"></i>
                                </div>
                            }
                        </div>
                    </div>
                </div>

                <div style="min-height:500px;">
                    <div class="block">
                        <!-- 产品列表 -->
                        <div id="activity_products_div" class="snap fn28">
                            <span id="status_title" class="fn-bold">火热抢购中...</span>
                            <div id="activity_countdown_div" class="fn-right">
                                <span class="col-gray">距活动结束</span>
                                <span class="time fn26">
                                    <em id="_h0">0</em><em id="_h1">0</em><i>:</i><em id="_m0">0</em><em id="_m1">0</em><i>:</i><em id="_s0">0</em><em id="_s1">0</em>
                                </span>
                            </div>
                        </div>
                        <div id="dataList" class="mod-pro-1 masonry">

                        </div>

                    </div>
                </div>

            }

            <div class="kefu">
                <div class="innerBox">
                    <p>微信客服</p>
                    <img src="~/Content/css/tokenmall/img/kefu.png" alt="">
                </div>
            </div>

        </div>
    </div>
    <!-- 置顶、购物车 -->
    <a class="goTop">
        <img src="~/Content/css/tokenmall/img/icon/goTop.png" />
    </a>
    <a href="javascript:void(0)" class="shoppingCart relative">
        <em class="absolute fn20" id="cartCount">0</em>
        <img src="~/Content/css/tokenmall/img/icon/shopppingCart.png" />
    </a>



    @section scripts{
        <script src="~/Scripts/tokenmall/swiper.min.js"></script>
        <script src="~/Scripts/mescroll/mescroll.min.js"></script>
        <script src="~/Scripts/token-common.js"></script>
    }
    @section style{
        <link rel="stylesheet" type="text/css" href="~/Content/css/tokenmall/css/swiper.min.css" media="all" />
        <link href="~/Scripts/mescroll/mescroll.min.css" rel="stylesheet" />
    }

    <!--活动选择-->
    <script type="text/javascript">
        var curActivityId = 0;
        var curActivityEndTime = '';
        var curActivityStatus = 0;
        $(function () {
            //活动产品数据加载
            var $container = $('.masonry');
            var total_pages = 1;
            var isload = false;
            let dataUrl = '/DistributionMall/_IndexActicityProductList';
            var mescroll = new MeScroll("mescroll", {
                down: {
                    auto: false,
                    callback: function (mescroll) {
                        mescroll.resetUpScroll();
                    }
                },
                up: {
                    callback: askData,
                    page: { num: 0, size: 6 },
                    clearEmptyId: "dataList",
                    isBounce: false,
                    noMoreSize: 3,
                    htmlNodata: '<p class="upwarp-nodata">亲,我是有底线的~</p >',
                    empty: { warpId: "dataList", icon: "", tip: "亲,暂时没有数据哦~" },
                    toTop: {}
                }
            });
            //异步请求数据
            function askData(pager) {
                if (curActivityId == undefined || curActivityId == 0) {
                    mescroll.endBySize([], 0);
                    return;
                }
                if (!isload && total_pages >= pager.num) {
                    isload = true;
                    $.ajax({
                        type: "post",
                        url: dataUrl,
                        params: "contentType",
                        /**/
                        data: { page: pager.num, size: pager.size, activityId: curActivityId },
                        /**/
                        async: true,
                        success: function (res) {
                            var $elems = $(res);
                            var paged = $elems.siblings("page");
                            var rows = Number.parseInt(paged.attr("rows"));
                            var total = Number.parseInt(paged.attr("total_result"));
                            mescroll.endBySize(rows, total);
                            if (paged.length == 1) {
                                total_pages = Number.parseInt(paged.attr("total_pages"));
                                $container.append($elems);
                                $('#dataList page').remove();
                            }
                            isload = false;
                        },
                        error: function (err) {
                            isload = false;
                            mescroll.endErr();
                        }
                    });
                }
            }


            $(".shoppingCart").click(function () {
                if (@merchant.id<= 0) {
                    var loginResult = isLogin();
                    if (!loginResult)
                        return loginResult;
                    location.href = "/home/login?returnUrl=@Request.Url"; return;
                }
                else {
                    location.href ="/cart/index";
                }
            })

            // 活动点击选中状态
            $('#activity_list_div .j-cur').click(function () {
                $(this).addClass('cur').siblings('.j-cur').removeClass('cur');

                    curActivityId = $(this).attr('data-id');
                    curActivityEndTime = $(this).attr('data-endtime');
                    curActivityStatus = $(this).attr('data-status');

                    if (curActivityStatus == 0) {//未开始
                        $("#activity_products_div #status_title").html('即将开始，敬请期待...');
                    $("#activity_products_div #activity_countdown_div").hide();

                    } else if (curActivityStatus == 1) {//进行中
                        $("#activity_products_div #status_title").html('火热抢购中...');
                    $("#activity_products_div #activity_countdown_div").show();
                        //执行倒计时
                        countTime();
                    } else if (curActivityStatus == 2) {//已结束
                        $("#activity_products_div #status_title").html('很遗憾，活动已结束...');
                    $("#activity_products_div #activity_countdown_div").hide();

                    }


                    //加载产品列表
                    //重置列表数据
                    mescroll.resetUpScroll();
                });

                //页面初始化，默认选择第一个进行中的活动
                setTimeout(function () {
                    if ($("#activity_list_div .underway_activity_item") != undefined && $("#activity_list_div .underway_activity_item").length > 0) {
                        $($("#activity_list_div .underway_activity_item")[$("#activity_list_div .underway_activity_item").length -1]).trigger('click');
                    } else if ($("#activity_list_div .activity_item_div") != undefined && $("#activity_list_div .activity_item_div").length > 0) {
                    $($("#activity_list_div .activity_item_div")[0]).trigger('click');
                    }
                }, 300);


                // 倒计时
                function checkTime(i) {
                    if (i < 10) {
                        i = '0' + i;
                    }
                    return i;
                }

                function countTime() {
                    //获取当前时间
                    var date = new Date();
                    var now = date.getTime();
                    //设置截止时间
                    var str = curActivityEndTime;
                    var endDate = new Date(str);
                    var end = endDate.getTime();
                    //时间差
                    var leftTime = end - now;
                    //定义变量 d,h,m,s保存倒计时的时间
                    var d, h, m, s;
                    if (leftTime >= 0) {
                        d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
                        h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
                        h += d * 24;
                        m = Math.floor(leftTime / 1000 / 60 % 60);
                        s = Math.floor(leftTime / 1000 % 60);
                    }
                    if (now > end) {
                        d = "00";
                        h = "00";
                        m = "00";
                        s = "00";
                        return;
                    }
                    d = checkTime(d);
                    h = checkTime(h);
                    m = checkTime(m);
                    s = checkTime(s);
                    h = String(h).split('');
                    m = String(m).split('');
                    s = String(s).split('');
                    // document.getElementById("_d").innerHTML = d+"天";
                    document.getElementById("_h0").innerHTML = h[0];
                    document.getElementById("_h1").innerHTML = h[1];
                    document.getElementById("_m0").innerHTML = m[0];
                    document.getElementById("_m1").innerHTML = m[1];
                    document.getElementById("_s0").innerHTML = s[0];
                    document.getElementById("_s1").innerHTML = s[1];
                    setTimeout(countTime, 1000);
                }

            })

    </script>

    <script>
        $(function () {
            //热卖
            var bannerSwiper = new Swiper('.mod-swiper', {
                loop: true,
                speed: 1000,
                autoplay: true,
                pagination: {
                    el: '.swiper-pagination',
                    // type: 'fraction',
                    // renderFraction: function(currentClass, totalClass) {
                    //     return '<span class="' + currentClass + '"></span>' + '/' + '<span class="' + totalClass + '"></span>';
                    // },
                },
            });

            //热卖
            var swiper = new Swiper('.j-activity', {
                slidesPerView: 'auto'
            });


        })
    </script>

    <script type="text/javascript">
        $(function () {
            $(".search_btn").click(function () {
                location.href = '/DistributionMall/Search';
            })
            // 客服弹窗
            $(document).click(function (e) {
                e.stopPropagation();
            })
            $('.contact').click(function () {
                $('.kefu').show();
            })
            $('.kefu').click(function () {
                $('.kefu').hide();
            })
            $('.kefu .innerBox').click(function (e) {
                e.stopPropagation();
            });
        });

        //防止后退
        var browser = {
            versions: function () {
                var u = navigator.userAgent, app = navigator.appVersion;
                return {
                    //移动终端浏览器版本信息
                    trident: u.indexOf('Trident') > -1, //IE内核
                    presto: u.indexOf('Presto') > -1, //opera内核
                    webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                    gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
                    mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
                    ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                    android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
                    iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
                    iPad: u.indexOf('iPad') > -1, //是否iPad
                    webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部,
                    WeiXin: u.match(/MicroMessenger/i) == "MicroMessenger" //是否为微信客户端
                };
            }(),
            language: (navigator.browserLanguage || navigator.language).toLowerCase()
        };

        //扫一扫
        function richScan() {
            try {
                if (browser.versions.ios) {
                    window.webkit.messageHandlers.richScan.postMessage("");
                } else if (browser.versions.android) {
                    androidfuntion.richScan();
                }
            } catch (e) {
                layer.msg("请下载最新版本APP，进行扫一扫");
            }
        }
        //扫一扫结果
        function getPhoneScanResult(scanResult) {
            //layer.msg("扫一扫结束，扫描内容：" + scanResult);
            if (scanResult.indexOf("http") != 0) {
                layer.msg("请扫描链接类型的二维码");
                return false;
            }
            window.location.href = scanResult;
        }
    </script>

    <!--更新购物车数量-->
    <script>
        //更新购物车数量
        var curUserId = Number('@merchant.id');
        function updateCartCount() {
            if (curUserId > 0) {
                $.get('/DistributionMall/GetCartCount', function (result) {
                    if (result.result_code == 1000) {
                        $("#cartCount").text(result.data);
                        if (Number(result.data) == 0) {
                            $("#cartCount").hide();
                        } else {
                            $("#cartCount").show();
                        }
                    }
                });
            }
        }
        $(function () {
                updateCartCount();

                document.addEventListener("visibilitychange", tabOnchange);
                document.addEventListener("mozvisibilitychange", tabOnchange);
                document.addEventListener("webkitvisibilitychange", tabOnchange);
                document.addEventListener("msvisibilitychange", tabOnchange);

                document.onfocusin = document.onfocusout = tabOnchange;

                window.onpageshow = window.onpagehide = window.onfocus = window.onblur = tabOnchange;

            var isLoadCartCount = false;
            function tabOnchange() {//激活tab页签，更新购物车数量
                if (isLoadCartCount) return;
                isLoadCartCount = true;
                setTimeout(function () { updateCartCount(); isLoadCartCount = false; }, 300);
            }

        });
    </script>
</body>

